package com.wscx.jingjie.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class CorsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest argo, ServletResponse arg1, FilterChain filterChain) throws IOException, ServletException {
        argo.setCharacterEncoding("utf-8");
        arg1.setCharacterEncoding("utf-8");
        HttpServletResponse httpResponse = (HttpServletResponse) arg1;

        String originHeader = ((HttpServletRequest) argo).getHeader("Origin");
        httpResponse.addHeader("Access-Control-Allow-Origin", "*");
        httpResponse.addHeader("Access-Control-Allow-Credentials", "true");
        httpResponse.setHeader("Access-Control-Allow-Headers", "Authentication,Origin, X-Requested-With, Content-Type, Accept,Token");
        httpResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");

        filterChain.doFilter(argo, arg1);
    }

    @Override
    public void destroy() {

    }
}